package com.air.ota.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.air.common.util.QueryCondition;
import com.air.common.util.QueryExpression;
import com.air.ota.mo.CancelTrans;
import com.air.ota.mo.InboundMsg;
import com.air.ota.service.ICancelConfirmHandlerService;
import com.air.ota.service.ICancelTransService;
import com.air.ota.service.ISMGPService;
import com.air.ota.util.AppConstant;
import com.air.ota.util.HexStringUtil;
import com.air.ota.util.SZTInterface;
import com.air.ota.vo.SZTBaseResponseVO;
import com.air.ota.vo.SZTCancelConfirmReqTransVO;
import com.air.ota.vo.SZTCancelConfirmRequestVO;
import com.air.ota.vo.SZTCancelConfirmResTransVO;
import com.air.ota.vo.UTKCancelConfirmUplinkVO;

@Service("cancelConfirmHandlerService")
public class CancelConfirmHandlerServiceImp  extends BaseUplinkMsgHandler 
	implements	ICancelConfirmHandlerService {
	
	@Autowired
	ICancelTransService cancelTransService;
	@Autowired
	ISMGPService smgpService;
	
	@Override
	public void proces(UTKCancelConfirmUplinkVO pkgVO, InboundMsg inboundMsg)
			throws Exception {
		System.out.println("------------Ready to Process Cancel Confirm Request");
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("cancelSequence",HexStringUtil.bytesToHexString(pkgVO.getCancelSequence()) , QueryExpression.EQUALS);
		condition.addQueryCondition("cellphone",inboundMsg.getPhoneNumber() , QueryExpression.EQUALS);
		List<CancelTrans> queryResult= cancelTransService.queryByCondition(condition);
		CancelTrans trans = queryResult.get(0);
		
		if ((trans.getResult()<0) || (trans.getResult()>=AppConstant._cancelResult_confirm_success)){
			 System.out.println("DISCARD CANCEL CONFIRM MSG,Cause Result="+trans.getResult());
			 return;
		}
		
		trans.setEwalletRemain(HexStringUtil.bytesToHexString(pkgVO.getEwalletRemain()));
		trans.setCardTransSequence(HexStringUtil.bytesToHexString(pkgVO.getCardTransSequence()));
		
		cancelTransService.updateById(trans);
		
		unlockRetryOutboundMsg(trans.getId(),AppConstant._outbound_msg_type_cancelResultResponse);
		
		//调用深圳通接口
		byte[] deviceSequence = HexStringUtil.hexStringToBytes(trans.getDeviceSequence());
		byte[] sztSequence = HexStringUtil.hexStringToBytes(trans.getSztId());
		byte[] result = HexStringUtil.hexStringToBytes("00000000");
		
		SZTCancelConfirmReqTransVO cancelConfirmReqTransVO = new SZTCancelConfirmReqTransVO(
				deviceSequence,sztSequence,trans.getDeviceTriggerTime(),
				HexStringUtil.hexStringToBytes(trans.getCardAppSequence()),
				result,
				HexStringUtil.hexStringToBytes(trans.getEwalletRemain()),
				HexStringUtil.hexStringToBytes(trans.getCardTransSequence())
				); 
		
		SZTCancelConfirmRequestVO requestVO = new SZTCancelConfirmRequestVO(cancelConfirmReqTransVO.getTransDataHexString());
		SZTInterface sztInterface = new SZTInterface();
		byte[] response=sztInterface.sendRequest(HexStringUtil.hexStringToBytes(requestVO.getMsgPackage()));
		SZTBaseResponseVO responseVO = new SZTBaseResponseVO();
		responseVO.parse(response);
		
		SZTCancelConfirmResTransVO sztCancelConfirmResTransVO = new SZTCancelConfirmResTransVO();
		sztCancelConfirmResTransVO.parse(HexStringUtil.hexStringToBytes(responseVO.getData()));
		
		String prompt;
		if (sztCancelConfirmResTransVO.getTransResult()==0x00){//Success
			System.out.println("success");
			trans.setResult(AppConstant._cancelResult_confirm_success);
			prompt ="天翼手机深圳通注销成功";
		}else{
			trans.setResult(AppConstant._cancelResult_confirm_failure);
			System.out.println("ERROR CODE="+HexStringUtil.byteToHexString(sztCancelConfirmResTransVO.getTransResult()));
			prompt = "天翼手机深圳通注销失败，请稍后重试";
		}
		this.cancelTransService.updateById(trans);
		this.smgpService.sendTxtSMS(trans.getCellphone(), prompt);
		
	}

}
